<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no'>
  <link rel='stylesheet' href='/css/style.css' />
  <script type="text/javascript" src="/js/axios.min.js"></script>
  <title>批量测试</title>
</head>
<body>
    <table>
    <tr><td>1.开启流程：</td><td>流程ID：<input id="processId">变量JSON：<textarea id="varsId" style="resize: both">[{"key":"count","value":0}]</textarea></td><td>发起数量：<input id="piCount" value="100"></td><td><input type="button" onclick="startPi();" value="开启流程"></td></tr>
    <tr><td>2.完成任务：</td><td><input type="button" onclick="getTaskIdsByProcessId()" value="获取流程任务ID"><span id="tasksCount"></span>任务：<textarea style="resize: both" id="taskIds" cols="50"></textarea></td>
	    <td colspan="1">任务类型：
	    <select id="cpType">
	    <option value="simple">普通任务</option>
	    <option value="appoint">完成并指定下节点处理人</option>
	    <option value="parti">竞争任务</option>
	    <option value="circulation">传阅任务</option>
	    </select></td><td>
	    <input type="button" onclick="completeTask()" value="完成任务"></td>
    </tr>
    <tr>
    <td>3.完成任务2：</td><td></td><td></td><td>
    <input type="button" onclick="completeTask2()" value="完成任务2"></td>
    </tr>
    </table>
</body>
<script type="text/javascript">
function startPi(){
	var url="uflo/rest/startProcess";//"/uflo/central/startProcess";
	var piCount=document.getElementById("piCount").value;
	if(!piCount){
		piCount=100;
	}
	
	var processId=document.getElementById("processId").value;
	if(!processId){
		alert("流程ID不能为空");
	}
	var vars=document.getElementById("varsId").value;
	var params="processId="+processId+"&piCount="+piCount;
	if(vars){
		vars=encodeURIComponent(vars);
		params+="&variables="+vars;
	}
	for(let i=0;i<piCount;i++){
		axios.post(url,params,{headers:{'Content-Type':'application/x-www-form-urlencoded','Authorization':'Basic dXNlcjpwd2Qx'}}).then(function(res){
			console.log(res);
		});
	}
	return;
	//window.open(url);
}
function getTaskIdsByProcessId(){
	var url="uflo/rest/getTaskIdsByProcessId?processId=";
	var processId=document.getElementById("processId").value;
	if(!processId){
		alert("流程ID不能为空");
	}
	url+=processId;
	axios.get(url,{headers:{'Authorization':'Basic dXNlcjpwd2Qx'}}).then(function(res){
		let taskIds=res.data.taskIds;
		let length=0;
		if(taskIds){
			length=taskIds.split(',').length;
		}
		document.getElementById("taskIds").value=taskIds;
		document.getElementById("tasksCount").innerHTML=length+"个";
	});
}
function completeTask(){
	const url="uflo/rest/completeTask";//uflo/central/completeTask";
	const taskIds=document.getElementById("taskIds").value;
	if(!taskIds){
		alert("任务ID不能为空");
	} else {
		var vars=document.getElementById("varsId").value;
		var params="";
		if(vars){
			vars=encodeURIComponent(vars);
			params+="&variables="+vars;
		}
		const ids=taskIds.split(',');
		ids.forEach(function(taskId){
			axios.post(url,"taskId="+taskId+params,{headers:{'Content-Type':'application/x-www-form-urlencoded','Authorization':'Basic dXNlcjpwd2Qx'}}).then(function(res){
				console.log(res);
			});
			return;
		});
		return;
	}
}
function completeTask2(){
	const url="completeTask2";//uflo/central/completeTask";
	const taskIds=document.getElementById("taskIds").value;
	if(!taskIds){
		alert("任务ID不能为空");
	} else {
		var vars=document.getElementById("varsId").value;
		axios.post(url,"taskIds="+taskIds+"&vars="+vars,{headers:{'Content-Type':'application/x-www-form-urlencoded','Authorization':'Basic dXNlcjpwd2Qx'}}).then(function(res){
			console.log(res);
		});
		return;
	}
}
</script>
</html>